package xyz.jcat.common.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

public class RegexUtils {

    private static final Map<String, Pattern> patternCache = new HashMap<>();

    public static boolean matches(String regex, String input) {
        return getPattern(regex).matcher(input).matches();
    }

    private static Pattern getPattern(String regex) {
        Pattern pattern = patternCache.get(regex);
        if(Objects.isNull(pattern)) {
            pattern = Pattern.compile(regex);
            patternCache.put(regex, pattern);
        }
        return pattern;
    }
}
